#include <bits/stdc++.h>

using namespace std;
using ll = long long;

bool check(ll x) {
	ll res = 0;
	ll y = x * x;
	while (y > 0) {
		res += y % 10;
		y /= 10;
	}
	return res == x;
}

void solve() {
	ll l, r;
	cin >> l >> r;

	ll mi = max(1LL, l);
	ll mx = min(26LL, r);
	ll ans = 0;
	for (ll i = mi; i <= mx; i++) {
		if (check(i)) {
			ans++;
		}
	}
	cout << ans << '\n';
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int t; cin >> t; while (t-- > 0) solve();
	return 0;
}